import 'package:flutter/material.dart';

abstract class MyState<W extends StatefulWidget> extends State<W> {
  @override
  void setState(VoidCallback fn) {
    if (mounted) super.setState(fn);
  }
}

class ApiResult<T> {
  // final bool isError;
  T? value;

  int state = -1;
  String message;

  ApiResult({this.state = -1, required this.message, this.value});

  factory ApiResult.ss(T t) =>
      ApiResult(state: 1, message: 'success', value: t);

  factory ApiResult.cc(String err) => ApiResult(state: 2, message: err);

  factory ApiResult.init() => ApiResult(message: "初始化", state: -1, value: null);

  bool isNotEmpty() => state == 2 && value != null;

  bool get isInit => state == -1;

  void startLoad() {
    if (isInit) state = 0;
  }

  void init() {
    state = -1;
    message = "初始化";
    value = null;
  }

  void success([T? t]) {
    state = 1;
    this.value = t;
    message = "成功";
  }

  void fail(String message) {
    state = 2;
    this.message = message;
  }

  bool get isLoading => state == 0;
  bool get isComplete => state > 0;
  bool get isError => state == 2;
  bool get isSuccess => state == 1;
}